<?php

namespace App\Admin\Controllers;

use App\Admin\Export\OrderCountExport;
use App\OrderPay;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class OrderCountController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('订单销售额统计');
            $content->description('订单销售额统计');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(OrderPay::class, function (Grid $grid) {

            $grid->model()
                ->select(\DB::raw("DATE(create_time) as day"),\DB::raw("sum(total_money) as num"))
                ->where('pay_status',1)
                ->where('refund_status','!=',OrderPay::REFUND_STATUS_APPLIED)
                ->groupBy(\DB::raw('DATE(create_time)'))
                 ->orderBy('create_time','desc');
            $grid->day('日期');
            $grid->num('销售额');
            $grid->disableActions();
            $grid->disableCreateButton();

            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });

            });
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->between('create_time', '下单时间')->datetime();

            });
            $grid->exporter(new OrderCountExport());

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(OrderPay::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
